package cn.com.ddapp.ddm.polling;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.view.Gravity;
import android.widget.Toast;

import com.google.gson.Gson;

import cn.com.ddapp.ddm.AppConfig;
import cn.com.ddapp.ddm.api.OKUtilAPI;
import cn.com.ddapp.ddm.base.BaseApplication;
import cn.com.ddapp.ddm.bean.order.AlipayCallbackEntity;
import cn.com.ddapp.ddm.bean.order.OrderPriceEntity;
import cn.com.ddapp.ddm.inter.OkUtilCallback;
import cn.com.ddapp.ddm.util.SPUtils;
import cn.com.ddapp.ddm.util.StringUtils;
import cn.com.ddapp.ddm.util.TLog;

/**
 * Created by caopenghui on 2016-09-10.
 */
public class PollingService extends Service {
    public static final String ACTION = "com.ryantang.service.PollingService";

    public static final String ALIPAY = "ALIPAY";
    public static final String WECAHT = "WECAHT";
    Gson gson = new Gson();
    private Notification mNotification;
    private NotificationManager mManager;
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    @Override
    public void onCreate() {
//        initNotifiManager();
    }

    @Override
    public void onStart(Intent intent, int startId) {
        try {
            String userId =  SPUtils.getStringData(this, SPUtils.USERID);
            String orderNo = intent.getStringExtra("orderNo");
            String type = intent.getStringExtra("type");
            if(StringUtils.isEmpty(orderNo))return;

            if(WECAHT.equalsIgnoreCase(type)){
                final OkUtilCallback okUtilCallback =  new OkUtilCallback() {
                    @Override
                    public void onMyResponse(String response, int id) {
                        TLog.log(ACTION,response);
                        OrderPriceEntity entity = gson.fromJson(response,OrderPriceEntity.class);
                        if((!AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult()))){
                            BaseApplication.getIntstance().showToast(entity.getResult(), Toast.LENGTH_SHORT,0, Gravity.CENTER);
                        }else{
                            Integer status = entity.getOrder().getStatus();
                            Intent intent1 = new Intent();
                            intent1.setAction(ACTION);
                            if(2==status){
                                intent1.putExtra("status",status+"");
                                sendBroadcast(intent1);
                            }
                        }

                    }
                };
                OKUtilAPI.post(OKUtilAPI.ORDER_PRICE, "userId="+userId+"&orderNum="+orderNo, okUtilCallback);
            }

            if(ALIPAY.equalsIgnoreCase(type)){
                final OkUtilCallback okUtilCallback =  new OkUtilCallback() {
                    @Override
                    public void onMyResponse(String response, int id) {
                        TLog.log(ACTION,response);
                        AlipayCallbackEntity entity = gson.fromJson(response,AlipayCallbackEntity.class);
                        if((!AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult()))){
                            BaseApplication.getIntstance().showToast(entity.getResult(), Toast.LENGTH_SHORT,0, Gravity.CENTER);
                        }else{
                            Intent intent1 = new Intent();
                            intent1.setAction(ACTION);
                            if(entity.isPay()){
                                intent1.putExtra("status",2+"");
                                sendBroadcast(intent1);
                            }
                        }

                    }
                };
                OKUtilAPI.post(OKUtilAPI.ALYPAY_CALLBACK, "orderNo="+orderNo, okUtilCallback);
            }

        }catch (Exception e){

        }
//        new PollingThread().start();
    }
//    //初始化通知栏配置
//    private void initNotifiManager() {
//        mManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//        int icon = R.drawable.ic_launcher;
//        mNotification = new Notification();
//        mNotification.icon = icon;
//        mNotification.tickerText = "New Message";
//        mNotification.defaults |= Notification.DEFAULT_SOUND;
//        mNotification.flags = Notification.FLAG_AUTO_CANCEL;
//    }
//    //弹出Notification
//    private void showNotification() {
//        mNotification.when = System.currentTimeMillis();
//        //Navigator to the new activity when click the notification title
//        Intent i = new Intent(this, MessageActivity.class);
//        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, i,
//                Intent.FLAG_ACTIVITY_NEW_TASK);
//        mNotification.setLatestEventInfo(this,
//                getResources().getString(R.string.app_name), "You have new message!", pendingIntent);
//        mManager.notify(0, mNotification);
//    }
    /**
     * Polling thread
     * 模拟向Server轮询的异步线程
     * @Author Ryan
     * @Create 2013-7-13 上午10:18:34
     */
    int count = 0;
    class PollingThread extends Thread {
        @Override
        public void run() {
            System.out.println("Polling...");
//            count ++;
//            //当计数能被5整除时弹出通知
//            if (count % 5 == 0) {
//                showNotification();
//                System.out.println("New message!");
//            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        System.out.println("Service:onDestroy");
    }
}
